/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package src.view.finance;

import java.io.Serializable;
import java.util.List;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.model.SelectItem;
import org.primefaces.context.RequestContext;
import org.primefaces.event.CloseEvent;
import src.domain.finance.Bank;
import src.domain.gl.Glaccnt;
import src.services.finance.BankModel;
import src.services.gl.CurrencyModel;
import src.services.gl.GlaccntConverter;
import src.services.gl.GlaccntModel;
import src.util.Messager;

/**
 *
 * @author jay
 */
@ManagedBean
@SessionScoped
public class BankBean implements Serializable {

    private List<Bank> banks;
    private List<SelectItem> avaibleGL, avaibleCurrency;
    private Glaccnt glaccount;
    private List<Glaccnt> avblAkun; 
    private Bank selectedData, insertedData;
    private boolean editMode = true;
    private GlaccntModel glHelper;
    private CurrencyModel curHelper;
    private BankModel helper;
    
    public BankBean() {
        this.helper = new BankModel();
        this.glHelper = new GlaccntModel();
        this.curHelper = new CurrencyModel();
        this.avaibleCurrency = curHelper.listAll();
        this.avaibleGL = glHelper.listAll();
        this.banks = helper.findAll(null);
        this.insertedData = null;
        this.selectedData = null;
        this.avblAkun = GlaccntConverter.glaccountDB;
    }

    public List<Glaccnt> getAvblAkun() {
        return avblAkun;
    }

    public Glaccnt getGlaccount() {
        return glaccount;
    }

    public void setGlaccount(Glaccnt glaccount) {
        this.glaccount = glaccount;
    }

    public boolean isEditMode() {
        return editMode;
    }
    
    public List<Bank> getBanks() {
        return this.banks;
    }
    
    public Bank getSelectedData() {
        return selectedData;
    }

    public void setSelectedData(Bank selectedData) {
        this.selectedData = selectedData;
    }
    
    public Bank getInsertedData() {
        return insertedData;
    }

    public void setInsertedData(Bank insertedData) {
        this.insertedData = insertedData;
    }

    public List<SelectItem> getAvaibleGL() {
        return avaibleGL;
    }

    public List<SelectItem> getAvaibleCurrency() {
        return avaibleCurrency;
    }
    
    public String getGLName(Integer id) {
        String nama = "";
        
        if (id!=null) {
            int idx=0;
            boolean found=false;
            while (idx<avaibleGL.size() && !found) {
                SelectItem it = avaibleGL.get(idx);
                found = id.equals(it.getValue()) ? true:false;
                if (found) nama = it.getLabel();
                idx++;
            }
        }
        return nama;
    }
    
    public String getCurrName(Integer id) {
        String nama = "";
        
        if (id!=null) {
            int idx=0;
            boolean found=false;
            while (idx<avaibleCurrency.size() && !found) {
                SelectItem it = avaibleCurrency.get(idx);
                found = id.equals(it.getValue()) ? true:false;
                if (found) nama = it.getLabel();
                idx++;
            }
        }
        return nama;
    }
        
    public void handleClose(CloseEvent event) {
        this.selectedData = null;
        this.insertedData = null;
        this.editMode = true;      
    }
    
    public void initForm() {
        this.avaibleCurrency = curHelper.listAll();
        this.avaibleGL = glHelper.listAll();
    }
    
    public void openData() { 
        initForm();
        this.editMode = true;
        insertedData = Bank.clone(selectedData);
        if (insertedData.getGlaccountId() != null) this.glaccount = glHelper.getById(insertedData.getGlaccountId()); 
    }
    
    public void newData() {  
        initForm();
        this.selectedData = null;
        this.editMode = false;
        this.insertedData = new Bank();
        this.glaccount = new Glaccnt();
    }
    
    public void save() {
        RequestContext context = RequestContext.getCurrentInstance();  
        insertedData.setGlaccountId(glaccount.getGlaccntId()); 
        boolean success = helper.saveData(insertedData);
        if (success==true) {
            if (this.editMode==false) this.banks.add(insertedData);
            else {
                int i = banks.indexOf(selectedData);
                if (i <= banks.size()) banks.set(i, Bank.clone(insertedData));
            }
            editMode = true;
            
        } else {
            if (this.editMode==false) insertedData.setBankId(null);
        } 
                
        if(!success) Messager.showSaveUpdateStatus(success);
        context.addCallbackParam("success", success);  
    }
    
    public void delete() {
        RequestContext context = RequestContext.getCurrentInstance();  
        
        boolean success = helper.deleteData(this.selectedData);
        if (success) {
            this.banks.remove(this.selectedData);
            this.selectedData = null;
        }
               
        if(!success) Messager.showDeleteStatus(success);
        context.addCallbackParam("success", success);  
    }
    
}
